﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

//【模板】前缀和
int main()
{
    int n, q;
    cin >> n >> q;
    vector<long long> nums(n + 1), sum(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> nums[i];
    }
    for (int i = 1; i <= n; i++)
    {
        sum[i] = sum[i - 1] + nums[i];
    }
    while (q--)
    {
        int l, r;
        cin >> l >> r;
        std::cout << sum[r] - sum[l - 1] << std::endl;
    }
    return 0;
}

//【模板】二维前缀和
int main()
{
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<long long>> matrix(n + 1, vector<long long>(m + 1));
    auto dp = matrix;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> matrix[i][j];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + matrix[i][j] - dp[i - 1][j - 1];
        }
    }
    int x1, y1, x2, y2;
    while (q--)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        std::cout << dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1] << std::endl;
    }
    return 0;
}

//寻找数组的中心下标
class Solution
{
public:
    int pivotIndex(vector<int>& nums)
    {
        int n = nums.size();
        if (n == 0) return -1;
        vector<int> dp(n);
        dp[0] = nums[0];
        for (int i = 1; i < n; i++)
        {
            dp[i] = dp[i - 1] + nums[i];
        }
        // 左端点
        if (dp[n - 1] - nums[0] == 0) return 0;
        for (int i = 1; i < n; i++)
        {
            if (dp[i - 1] == dp[n - 1] - dp[i])
                return i;
        }
        // 右端点
        if (dp[n - 2] == 0) return n - 1;
        return -1;
    }
};

class Solution
{
public:
    int pivotIndex(vector<int>& nums)
    {
        int n = nums.size();
        if (n == 0) return -1;
        vector<int> lsum(n), rsum(n);
        // lsum[i] 表⽰：[0, i - 1] 区间所有元素的和
        // rsum[i] 表⽰：[i + 1, n - 1] 区间所有元素的和
        for (int i = 1; i < n; i++)
        {
            lsum[i] = lsum[i - 1] + nums[i - 1];
        }
        for (int i = n - 2; i >= 0; i--)
        {
            rsum[i] = rsum[i + 1] + nums[i + 1];
        }
        for (int i = 0; i < n; i++)
        {
            if (lsum[i] == rsum[i]) return i;
        }
        return -1;
    }
};

//除自身以外数组的乘积
class Solution
{
public:
    vector<int> productExceptSelf(vector<int>& nums)
    {
        // lprod 表⽰：[0, i - 1] 区间内所有元素的乘积
        // rprod 表⽰：[i + 1, n - 1] 区间内所有元素的乘积
        int n = nums.size();
        vector<int> lprod(n), rprod(n);
        lprod[0] = 1, rprod[n - 1] = 1;
        for (int i = 1; i < n; i++)
        {
            lprod[i] = lprod[i - 1] * nums[i - 1];
        }
        for (int i = n - 2; i >= 0; i--)
        {
            rprod[i] = rprod[i + 1] * nums[i + 1];
        }

        vector<int> ans(n);
        for (int i = 0; i < n; i++)
        {
            ans[i] = lprod[i] * rprod[i];
        }

        return ans;
    }
};

//和为K的子数组
class Solution
{
public:
    int subarraySum(vector<int>& nums, int k)
    {
        // 前缀和出现的次数
        unordered_map<int, int> hash;
        // [0,i]和为k，此时就需要在hash表中找[0,-1]区间和为0
        // 所以初始化hash[0] = 1
        // 哈希表只存[0,i-1]之前的和
        hash[0] = 1;
        int sum = 0, ans = 0;
        for (auto x : nums)
        {
            sum += x;// 当前位置的前缀和
            // 查找前面前缀和等于sun-k的个数
            if (hash.count(sum - k))
            {
                ans += hash[sum - k];
            }
            hash[sum]++;
        }

        return ans;
    }
};

//和可被K整除的子数组
class Solution
{
public:
    int subarraysDivByK(vector<int>& nums, int k)
    {
        // 记录前缀和余数为k的个数
        unordered_map<int, int> hash;
        hash[0 % k] = 1;
        int ans = 0, sum = 0;
        for (auto x : nums)
        {
            sum += x;
            // 考虑负数%正数的情况
            // 我们要对取余的结果+k之后再对k取余
            // (a - b) % k == 0
            // a % k == b % k
            int r = (sum % k + k) % k;
            if (hash.count(r)) ans += hash[r];
            hash[r]++;
        }

        return ans;
    }
};


//连续数组
class Solution
{
public:
    int findMaxLength(vector<int>& nums)
    {
        // 我们将0变为-1，那么这道题就变成了连续的和为0的子数组的最长长度
        // 我们使用哈希表记录前缀和以及出现的下标
        unordered_map<int, int> hash;
        hash[0] = -1;
        int ans = 0, sum = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            sum += nums[i] == 0 ? -1 : 1;
            if (hash.count(sum)) ans = max(ans, i - hash[sum]);
            //因为是求最长的长度,所以只记录第一次出现的下标
            else hash[sum] = i;
        }

        return ans;
    }
};

class Solution
{
public:
    vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k)
    {
        int m = mat.size(), n = mat[0].size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
            }
        }
        vector<vector<int>> ans(m, vector<int>(n));
        // 转换为计算 [x1,y1]和[x2,y2]区间的和
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                // 计算边界
                int x1 = max(0, i - k) + 1, y1 = max(0, j - k) + 1;
                int x2 = min(m - 1, i + k) + 1, y2 = min(n - 1, j + k) + 1;
                ans[i][j] = dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1];
            }
        }

        return ans;
    }
};